<?php


class Crawl_tencent_keyword_from_html_library  extends Crawl_keyword_library{

    private $_url;
    private $_cookie;
    private $_parameters = '';
    public  $_htmlsearch_config;
    public  $_null_tab;

    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['tencent'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];
        $this->_no_login    = $this->_htmlsearch_config['no_login'];
        $this->_restricted_tag = $this->_htmlsearch_config['restricted_tag'];
        $this->_illegal_tag  = $this->_htmlsearch_config['illegal_tag'];
        $this->_result_error = $this->_htmlsearch_config['result_error'];
        $this->_result_success = $this->_htmlsearch_config['result_success'];
        $this->_url         = $this->_htmlsearch_config['url'];
        $this->_pos         = $this->_htmlsearch_config['pos'];
        $this->_s_dup       = $this->_htmlsearch_config['s_dup'];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {
        $crawl_result = $this->try_crawl($retry);
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else if(is_array($crawl_result) && array_key_exists('data', $crawl_result) && is_null($crawl_result['data'])) {
            return $crawl_result;
        } else {
            $return_data = $this->make_data($crawl_result);
            if($return_data === FALSE) {
                $return_data = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
            }
            return $return_data;
        }
    }

    private function make_data($result = NULL) {
        $this->_ci->load->model('split/htmlsearchparser_model');
        $split_data = $this->_ci->htmlsearchparser_model->parser_tencent($this->_htmlsearch_config, $result);
        if($split_data == FALSE) {
            $error = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }
        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */

    private function try_crawl($retry = FALSE) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters();

        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Tencent Html Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Tencent Html Un Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            //return self::get_content($parameters);
            $content = self::get_content($parameters);
            
            
            if(is_array($content) && isset($content['error']) && $content['error'] != ""){
            	$this->label_cookie($this->_cookie['id']);
            }else{
            	$this->match_html($content);
            }
            
            return $content;
        } else {
//             $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['tencent_html'], TRUE);
//             if(is_array($crawl_use_proxy_ip_result) && $this->not_illegal_error($crawl_use_proxy_ip_result)) {
//                 if(!array_key_exists($this->_error_string, $crawl_use_proxy_ip_result) && !array_key_exists($this->_null_data_array_key, $crawl_use_proxy_ip_result)) {
//                     $temp_array = $crawl_use_proxy_ip_result;
//                 }

                return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['tencent_html'], TRUE);

//                 if(is_array($re_crawl_result) && !array_key_exists($this->_error_string, $re_crawl_result) && !array_key_exists($this->_null_data_array_key, $re_crawl_result)) {
//                     return $this->make_crawl_return_array($temp_array, $re_crawl_result);
//                 } else if(count($temp_array) > 0) {
//                     return $this->make_crawl_return_array($temp_array);
//                 }else {
//                     return $re_crawl_result;
//                 }
//             } else {
//                 return $crawl_use_proxy_ip_result;
//             }
        }
    }

    /**
     * 抓取内容验证
     * @param $html
     * @return bool
     */
    public function match_html($html) {
        if(strpos($html, $this->_no_login)) {
            //如果返回登陆页 或 被禁 标记cookie 为不可用
            $this->label_cookie($this->_cookie['id']);
            //如果返回登陆页 或 被禁 重新组织参数 更新COOKIE
            $this->make_parameters();
            return $this->_ci->error_code_library->getError('curl', 'data', 'cookie_error', 'From '.$this->_crawl_from['tencent_html'].$this->_no_login);
        } else if(strpos($html, $this->_restricted_tag)) {
            //如果返回登陆页 或 被禁 标记cookie 为不可用
            $this->label_cookie($this->_cookie['id']);
            //如果返回登陆页 或 被禁 重新组织参数 更新COOKIE
            $this->make_parameters();
            return $this->_ci->error_code_library->getError('curl', 'data', 'cookie_disabled', 'From '.$this->_crawl_from['tencent_html'].$this->_restricted_tag);
        } elseif(strpos($html, $this->_illegal_tag)) {
            //关键词被对应平台判断为非法
            return $this->_ci->error_code_library->getError('curl', 'data', 'illegal', 'From '.$this->_crawl_from['tencent_html'].$this->_illegal_tag);
        } elseif(strpos($html, $this->_result_error)) {
            $this->label_proxy_table($this->_proxy_ip, $this->_use_proxy);
            return $this->_ci->error_code_library->getError('curl', 'data', 'verification_code', 'From '.$this->_crawl_from['tencent_html'].$this->_result_error);
        } elseif(strpos($html, $this->_result_success) === FALSE) {
            return $this->_ci->error_code_library->getError('curl', 'data', 'incomplete_answers', 'From '.$this->_crawl_from['tencent_html'].$this->_result_success);
        } else {
            return TRUE;
        }
    }


    private function make_parameters() {
    	//若传入cookie信息.则使用传入的cookie信息,用于调试cookie 是否有效使用 add by hejigui
    	$post_cookie = trim($this->_ci->input->post('cookie_str'));
        if(!empty($post_cookie)){
        	$cookie['cookie_str'] = $post_cookie;
        }else{
			$cookie = $this->get_cookie();
        }

        if(is_array($cookie) && array_key_exists($this->_error_string, $cookie)) {
            return $cookie;
        }

        $this->_cookie = $cookie;
        $parameters['url'] = $this->_url;
        $parameters['method'] = self::METHOD_GET;

        $parameters['postData']['k'] = trim($this->_ci->input->post('word'));
        $parameters['postData']['p'] = $this->get_page();
        $parameters['postData']['pos'] = $this->_pos;
        $parameters['postData']['s_dup'] = $this->_s_dup;

        $parameters['cookie_str'] = true;
        $parameters['cookie'] = $this->_cookie['cookie_str'];
        $this->update_cookie_use_count($cookie['id']);
        return $this->_parameters = $parameters;
    }

}
